package com.htfyun.fragment.library.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.htfyun.Constants;
import com.htfyun.fragment.library.TreeFragment;
import com.htfyun.fragment.listener.OnRecyclerViewItemClickListener;
import com.htfyun.utils.DebugLog;
import com.htfyun.utils.DisplayUtil;

import org.fbreader.util.android.ViewUtil;
import org.geometerplus.android.fbreader.covers.CoverManager;
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.android.fbreader.tree.TreeActivity;
import org.geometerplus.fbreader.Paths;
import org.geometerplus.fbreader.book.Author;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.fbreader.library.AuthorListTree;
import org.geometerplus.fbreader.library.AuthorTree;
import org.geometerplus.fbreader.library.ExternalViewTree;
import org.geometerplus.fbreader.library.FavoritesTree;
import org.geometerplus.fbreader.library.FileFirstLevelTree;
import org.geometerplus.fbreader.library.FileTree;
import org.geometerplus.fbreader.library.LibraryTree;
import org.geometerplus.fbreader.library.RecentBooksTree;
import org.geometerplus.fbreader.library.SearchResultsTree;
import org.geometerplus.fbreader.library.SyncTree;
import org.geometerplus.fbreader.library.TagListTree;
import org.geometerplus.fbreader.library.TagTree;
import org.geometerplus.fbreader.library.TitleListTree;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filetypes.FileType;
import org.geometerplus.zlibrary.core.filetypes.FileTypeCollection;
import org.geometerplus.zlibrary.core.util.RationalNumber;
import org.geometerplus.zlibrary.ui.android.R;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by sst on 2017/11/1.
 */

public class LibraryRecyclerViewAdapter extends TreeRecyclerViewAdapter<LibraryRecyclerViewAdapter.MyViewHolder> {

    private CoverManager myCoverManager;

    private LayoutInflater mInflater;

    private OnRecyclerViewItemClickListener mClickListener;


    public LibraryRecyclerViewAdapter(SupportFragment fragment) {
        super(fragment);
        mInflater = LayoutInflater.from(getContext());

    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = mInflater.inflate(R.layout.htfyun_library_tree_item, parent, false);

        if (myCoverManager == null) {
//            view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            final int coverHeight = DisplayUtil.dipToPix(getContext(), Constants.COVER_HEIGHT);//view.getMeasuredHeight();
            final int coverWidth = DisplayUtil.dipToPix(getContext(), Constants.COVER_WIDTH);//view.getMeasuredHeight();
            final TreeFragment fragment = (TreeFragment)getMyFragment();
            myCoverManager = new CoverManager(fragment.getActivity(), fragment.ImageSynchronizer, coverWidth, coverHeight);
//            view.requestLayout();
        }
        
        final MyViewHolder holder = new MyViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if (mClickListener != null) {
                    mClickListener.onItemClick(position, v, holder);
                }
            }
        });
        return holder;
    }

    
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        final LibraryTree tree = (LibraryTree)getItem(position);
        Book book = tree.getBook();
        if (book != null) {
            holder.itemName.setText(tree.getName());

            holder.itemAuthor.setText(book.authorsName());

            holder.progressBarPagePercent.setMax(100);
            holder.progressBarPagePercent.setProgress(0);
            
            RationalNumber rationalNumber = book.getProgress();

            if (rationalNumber != null) {

                holder.progressBarPagePercent.setMax((int) book.getProgress().Denominator);
                holder.progressBarPagePercent.setProgress((int) book.getProgress().Numerator);
            }

            final ImageView coverView = holder.itemIcon;
            if (!myCoverManager.trySetCoverImage(coverView, book, PluginCollection.Instance(Paths.systemInfo(getContext())))) {
                
                coverView.setImageResource(getCoverResourceId(book));
            }
            
        }
        
        
        
    }

    private int getCoverResourceId(Book book) {
        if (book != null) {

            final FileType fileType = FileTypeCollection.Instance.typeForFile(ZLFile.createFileByPath(book.getPath()));
            DebugLog.e("fileTyp.id = " + fileType.Id);
            if (fileType.Id.equalsIgnoreCase("TXT")) {
                return (R.drawable.ic_shelf_txt);
            } else if (fileType.Id.equalsIgnoreCase("EPUB")) {
                return (R.drawable.ic_shelf_epub);
            }

        }

        return (R.drawable.ic_shelf_txt);

    }
  

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView itemIcon;
        TextView itemName;
        TextView itemAuthor;
        ProgressBar progressBarPagePercent;

        public MyViewHolder(View itemView) {
            super(itemView);
            itemIcon = (ImageView)itemView.findViewById(R.id.library_tree_item_icon);
            itemName = (TextView) itemView.findViewById(R.id.library_tree_item_name);
            itemAuthor = (TextView) itemView.findViewById(R.id.library_tree_item_author);
            progressBarPagePercent = (ProgressBar) itemView.findViewById(R.id.progressBarPagePercent);
        }
    }

}
